const { sequelize } = require('../utils/sequelize');
const { v4: uuidv4 } = require('uuid');

class ClassRoomService {
    constructor() {
    }

    async findAll(pageSize, pageNum, level){
        let data = null;
        let offset = pageSize * (pageNum - 1);
        try {
            data = await sequelize.models.ClassRoom.findAndCountAll({
                attributes: ['classRoomId', 'classRoomAddr'],
                offset,
                limit: pageSize * 1,
                where: {
                    classRoomLevel: 1
                }
            });
            if(!level){
                let dataRows = data.rows;
                for(let i=0; i<dataRows.length;i++){
                    let childRoomArr = await sequelize.models.ClassRoom.findAll({
                        attributes: ['classRoomId', 'classRoomAddr'],
                        where: {
                            classRoomLevel: 2,
                            lastLevelId: dataRows[i].classRoomId
                        }
                    })
                    dataRows[i].setDataValue('child', childRoomArr)
                }
            }
            
        } catch (error) {
            error['name'] = error.name || '未知错误'
            console.log(error)
            return error;
        }
        return data;
    }

    async findById(roomId){
        let data = null;
        // console.log(roomId)
        try {
            let roomObj
            roomObj = await sequelize.models.ClassRoom.findOne({
                where: {classRoomId: roomId}
            });
            // console.log(roomObj)
            // console.log(roomObj.getDataValue('lastLevelId'))
            data = await sequelize.models.ClassRoom.findOne({
                attributes: ['classRoomId', 'classRoomAddr'],
                where: {
                    classRoomLevel: 1,
                    classRoomId: roomObj.getDataValue('lastLevelId')
                }
            })
            data.setDataValue('child', roomObj);
        } catch (error) {
            error['name'] = error.name || '未知错误'
            console.log(error)
            return error;
        }
        
        return data;
    }

    async getAddr(roomId) {
        let addr = null;
        try {
            let roomObjTwo, roomObjOne;
            roomObjTwo = await sequelize.models.ClassRoom.findOne({
                where: {classRoomId: roomId}
            });
            roomObjOne = await sequelize.models.ClassRoom.findOne({
                attributes: ['classRoomId', 'classRoomAddr'],
                where: {
                    classRoomLevel: 1,
                    classRoomId: roomObjTwo.getDataValue('lastLevelId')
                }
            })
            addr = roomObjOne.getDataValue('classRoomAddr') + roomObjTwo.getDataValue('classRoomAddr')
        } catch (error) {
            error['name'] = error.name || '未知错误'
            console.log(error)
            return error;
        }
        
        return addr;
    }

    async saveRoom(roomObj){
        let data = null;

        try {
            data = await sequelize.models.ClassRoom.create({
                classRoomId: uuidv4(),
                classRoomAddr: roomObj.classRoomAddr,
                classRoomLevel: roomObj.classRoomLevel,
                lastLevelId: roomObj.lastLevelId
            })
        } catch (error) {
            error['name'] = error.name || '未知错误'
            console.log(error)
            return error;
        }
        return data;
    }

    async deleteRoom(roomId){
        let data = null;
        try {
            let hasData = await sequelize.models.ClassRoom.findOne({
                where: {
                    lastLevelId: roomId
                }
            })
            if(hasData) return {name: '该地点还存在教室'}
            data = await sequelize.models.ClassRoom.destroy({where: {class_room_id: roomId}});
        } catch (error) {
            error['name'] = error.name || '未知错误'
            console.log(error)
            return error;
        }
        return data;
    }

    async updateRoom(room){
        let data = null;
        let roomObj = {
            classRoomAddr: room.classRoomAddr,
        }
        try {
            data = await sequelize.models.ClassRoom.update({...roomObj}, {
                where: {class_room_id: room.classRoomId}
            })
        } catch (error) {
            error['name'] = error.name || '未知错误'
            console.log(error)
            return error;
        }
        return data;
    }
}

module.exports = new ClassRoomService();